<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="auto"
    >
      <el-form-item label="用户名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动/竞赛/名称" prop="activitysName">
        <el-input
          v-model="queryParams.activitysName"
          placeholder="请输入活动/竞赛/名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报名进度" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择报名进度">
          <el-option :value="0" label="已提交">已提交</el-option>
          <el-option :value="1" label="待提交">待提交</el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="详情类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择报名进度">
          <el-option :value="'0'" label="竞赛">竞赛</el-option>
          <el-option :value="'1'" label="活动">活动</el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="报名时间" prop="enrollTime">
        <el-date-picker
          clearable
          v-model="queryParams.enrollTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择报名时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['apply:info:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['apply:info:remove']"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="infoList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="报名记录id" align="center" prop="id" />
      <el-table-column label="用户名称" align="center" prop="userName" />
      <el-table-column
        label="活动/竞赛/标题"
        align="center"
        prop="activitysName"
      />
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          {{ scope.row.type === "0" ? "竞赛" : "活动" }}
        </template>
      </el-table-column>
      <el-table-column
        label="报名时间"
        align="center"
        prop="enrollTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            scope.row.status == 0
              ? parseTime(scope.row.enrollTime, "{y}-{m}-{d}")
              : "还未提交"
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="报名进度" align="center" prop="status">
        <template slot-scope="scope">
          {{ scope.row.status == 0 ? "已提交" : "待提交" }}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['apply:info:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['apply:info:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改报名信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="750px" append-to-body>
      <div v-show="detailStatus == 0">
        <el-form ref="form" :model="form" :rules="rules" label-width="auto">
          <el-form-item label="用户名称" prop="userId">
            <el-input
              disabled
              v-model="form.userName"
              placeholder="请输入用户id"
            />
          </el-form-item>
          <el-form-item label="用户详情报名信息">
            <!-- <editor v-model="form.content" :min-height="192" /> -->
            <el-button v-if="form.content != null" @click="buttonApply()"
              >点击查看报名详情</el-button
            >
            <el-tag type="warning" v-else
              >该用户未使用系统模板，不提供导出功能 请联系赛方获取信息</el-tag
            >
          </el-form-item>
          <el-form-item label="活动/竞赛/名称" prop="activityId">
            <el-input
              disabled
              v-model="form.activitysName"
              placeholder="请输入活动/竞赛/id"
            />
          </el-form-item>
          <el-form-item label="报名时间" prop="enrollTime">
            <el-date-picker
              disabled
              clearable
              v-model="form.enrollTime"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择报名时间"
            >
            </el-date-picker>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </div>
      <div v-show="detailStatus == 1">
        <el-form :inline="true" label-width="auto">
          <span v-for="(item, index) in applyInfoList" :key="index">
            <el-form-item :label="item.name">
              <component
                disabled
                :is="item.type === 0 ? 'el-input' : 'el-select'"
                v-model="item.value"
              >
                <!-- <el-option v-if="item.type === 1" v-for="option in item.options" :key="option.value" :label="option.label" :value="option.value"></el-option> -->
              </component>
            </el-form-item>
          </span>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport()"
            v-hasPermi="['apply:info:export']"
            >导出</el-button
          >
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, updateInfo } from "@/api/apply/info";

export default {
  name: "Info",
  data() {
    return {
      //状态控制
      detailStatus: 0,
      //报名详情信息
      applyInfoList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报名信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        content: null,
        activityId: null,
        activitysName: null,
        userName: null,
        type: null,
        enrollTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //详情按钮
    buttonApply() {
      this.detailStatus = 1;
      this.applyInfoList = this.convertJsonToArray(this.form.content);
      console.log(this.applyInfoList);
    },
    // 定义一个方法来将JSON字符串转换为数组对象
    convertJsonToArray(jsonString) {
      try {
        // 使用JSON.parse将JSON字符串转换为JavaScript对象
        const jsonObject = JSON.parse(jsonString);

        // 检查转换后的对象是否已经是数组，如果不是则根据实际情况处理
        if (!Array.isArray(jsonObject)) {
          console.warn("报名信息异常请及时处理");
          return [];
        }

        // 如果转换成功且是数组，则直接返回
        return jsonObject;
      } catch (error) {
        // 处理解析错误
        console.error("解析JSON字符串时发生错误:", error);
        return [];
      }
    },
    /** 查询报名信息列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then((response) => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        content: null,
        activityId: null,
        type: null,
        enrollTime: null,
        isDel: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加报名信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.detailStatus = 0;
      this.reset();
      const id = row.id || this.ids;
      getInfo(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "报名信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除报名信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delInfo(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      console.log(this.form.id);
      this.download(
        "/admin/apply/info/export",
        { ...this.form },
        `用户报名信息.xlsx`
      );
    },
  },
};
</script>
